package com.yy.finvideoplayer.renderview

import android.annotation.TargetApi
import android.content.Context
import android.graphics.Bitmap
import android.graphics.SurfaceTexture
import android.os.Build
import android.util.AttributeSet
import android.util.Log
import android.view.Surface
import android.view.SurfaceHolder
import android.view.TextureView
import android.view.View
import com.yy.finvideoplayer.media.IMediaControl

/**
 * @Description: TextureRenderView
 * @Author: yiw89
 * @CreateDate: 2025/9/1 15:00
 * @Version: 1.0
 */
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
class TextureRenderView : TextureView, TextureView.SurfaceTextureListener,
    IRenderView {
    private lateinit var mMeasureHelper: MeasureHelper
    private var videoWidth = 0
    private var videoHeight = 0

    private var callback: IRenderView.IRenderCallback? = null

    constructor(context: Context?) : super(context!!) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(
        context!!, attrs
    ) {
        init()
    }

    private fun init() {
        surfaceTextureListener = this
        mMeasureHelper = MeasureHelper(this)
    }


    override fun get(): View {
        return this
    }

    override fun setVideoSize(videoWidth: Int, videoHeight: Int) {
        if (videoWidth >= 0 && videoHeight >= 0) {
            this.videoWidth = videoWidth
            this.videoHeight = videoHeight
            mMeasureHelper.setVideoSize(videoWidth, videoHeight)
            requestLayout()
        }
    }

    override fun setAspectRatio(aspectRatio: Int) {
        mMeasureHelper.setAspectRatio(aspectRatio)
        requestLayout()
    }

    override fun getSurfaceHolder(): SurfaceHolder? {
        return null
    }

    override fun setVideoRotation(degree: Int) {
        if (degree.toFloat() != rotation) {
            super.setRotation(degree.toFloat())
            mMeasureHelper.setVideoRotation(degree)
            requestLayout()
        }
    }

    override fun addRenderCallback(callback: IRenderView.IRenderCallback?) {
        this.callback = callback
    }

    override fun removeRenderCallback() {
        this.callback = null
    }

    override fun openSurface(): Surface? {
        if (surface != null) return Surface(surface)
        return null
    }

    override fun bindMedia(iMediaControl: IMediaControl) {
        openSurface()?.let { iMediaControl.setSurface(it) }
    }

    override fun getCurrentFrame(): Bitmap? {
        return bitmap
    }

    private var surface: SurfaceTexture? = null

    //    @TargetApi(16)
    //改变大小时会重建Surface
    override fun onSurfaceTextureAvailable(surface: SurfaceTexture, width: Int, height: Int) {
        Log.i(
            TAG,
            "onSurfaceTextureAvailable $width-$height"
        )
        if (this.surface == null || Build.VERSION.SDK_INT < 16) {
            this.surface = surface
            callback?.onSurfaceCreated(this, width, height)
        } else {
            //当api大于16 不用重新set解码器的suface 把原来的更新进view就行
            setSurfaceTexture(this.surface!!)
        }
    }

    override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture, width: Int, height: Int) {
        Log.i(
            TAG,
            "onSurfaceTextureSizeChanged $width-$height"
        )
        callback?.onSurfaceChanged(this, 0, width, height)
    }

    //返回值待研究...
    override fun onSurfaceTextureDestroyed(surface: SurfaceTexture): Boolean {
        Log.i(TAG, "onSurfaceTextureDestroyed")
        callback?.onSurfaceDestroyed(this)
        return this.surface == null
    }

    override fun onSurfaceTextureUpdated(surface: SurfaceTexture) {
        //Log.i(TAG, "onSurfaceTextureUpdated");
    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        mMeasureHelper.doMeasure(widthMeasureSpec, heightMeasureSpec)
        setMeasuredDimension(mMeasureHelper.getMeasuredWidth(), mMeasureHelper.getMeasuredHeight())
    }


    companion object {
        protected const val TAG: String = "TextureRenderView"
    }
}
